package com.barry.retryer;

import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.google.common.base.Predicates;

import java.util.concurrent.TimeUnit;

import static com.github.rholder.retry.WaitStrategies.incrementingWait;

/**
 * @author wangxuexing
 * @descrption
 * @date
 */
public class RetryDemo {
    public static void main(String[] args) {
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder().
                                    //如果异常会重试
                                    retryIfException().
                                    //如果结果为false会重试
                                    retryIfResult(Predicates.equalTo(false)).
                                    //重调策略
                                    withWaitStrategy(incrementingWait(30, TimeUnit.SECONDS, 30, TimeUnit.SECONDS)).
                                    //尝试次数
                                    withStopStrategy(StopStrategies.stopAfterAttempt(3)).
                                    //注册监听
                                    withRetryListener(new MyRetryListener()).build();
        try {
            retryer.call(new TaskCallable());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
